package com.example.campusrear.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.campusrear.Service.*;
import com.example.campusrear.Utils.R;
import com.example.campusrear.entity.*;
import com.example.campusrear.entity.Class;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Resource
    private CourseService courseService;
    @Resource
    private StudentCourseService studentCourseService;

    @Resource
    private StudentService studentService;
    @Resource
    private ClassCourseService classCourseService;

    @Resource
    private CourseAttendanceService courseAttendanceService;  //考勤

    @Resource
    private AbsentStudentService absentStudentService;  //缺勤

    @Resource
    private CourseCategoryService courseCategoryService;

    @GetMapping("/page")
    public R<Page<Course>> page(int page,int pageSize,String name,Long cId,String semester){
        Page<Course> coursePage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        if (cId!=null){
            wrapper.eq(Course::getClassId,cId);
        }
        if (semester!=null){
            wrapper.like(Course::getSemester,semester);
        }
        wrapper.like(StringUtils.isNotEmpty(name),Course::getCourseName,name);
        wrapper.orderByAsc(Course::getSort).orderByAsc(Course::getCourseTimes);
        courseService.page(coursePage,wrapper);
        return R.success(coursePage);
    }
    @GetMapping("/studentCourse")
    public R<List<Course>> findStudentCourse(Long sId){
        LambdaQueryWrapper<StudentCourse> wrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Course> courseWrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentCourse::getStudentId,sId);
        List<StudentCourse> list = studentCourseService.list(wrapper);
        List<Course> courses = new ArrayList<>();
        for (StudentCourse studentCourse : list) {
            Long courseId = studentCourse.getCourseId();
            courseWrapper.eq(Course::getCourseId,courseId);
            Course course = courseService.getOne(courseWrapper);
            courses.add(course);
        }
        if (courses.size()!=0){
            return R.success(courses);
        }
        return R.error("该学生无课表");
    }

    @PostMapping("/upload")
    public R<String> upload(@RequestBody Course course){
        String courseTime = course.getCourseTime();
        List<Course> lists = courseService.list();
//        for (Course list : lists) {
//            if (courseTime.equals(list.getCourseTime())){
//                return R.error("该时间已有课程，不能修改");
//            }
//        }
        LambdaQueryWrapper<Course> courseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseLambdaQueryWrapper.eq(Course::getCourseId,course.getCourseId());
        courseService.update(course,courseLambdaQueryWrapper);
        return R.success("修改成功");
    }
    @DeleteMapping("/delete/{cId}")   //删除课程
    public R<String> delete(@PathVariable Long cId){
        courseService.removeById(cId);
        return R.success("删除成功");
    }
    @DeleteMapping("/deleteAtt/{aId}")
    public R<String> deleteAtt(@PathVariable Long aId){
        courseAttendanceService.removeById(aId);
        return R.success("删除成功");
    }
    @PostMapping("/save")
    public R<String> save(@RequestBody Course course){
        String courseTime = course.getCourseTime();
        String substring = courseTime.substring(0,2);
        switch (substring){
            case "周一":
                course.setSort(1);
                break;
            case "周二":
                course.setSort(2);
                break;
            case "周三":
                course.setSort(3);
                break;
            case "周四":
                course.setSort(4);
                break;
            case "周五":
                course.setSort(5);
                break;
            case "周六":
                course.setSort(6);
                break;
            case "周日":
                course.setSort(7);
                break;
        }
        List<Course> lists = courseService.list();
        for (Course list : lists) {
            if (courseTime.equals(list.getCourseTime())){
                return R.error("该时间已有课程，不能再添加");
            }
        }
        courseService.save(course);
        return R.success("添加成功");
    }

    //为此课程添加考勤
    @PostMapping("/Attendance")
    public R<String> saveAttendance(@RequestBody CourseAttendance courseAttendance){
        courseAttendanceService.save(courseAttendance);
        return R.success("操作成功");
    }

    @PostMapping("/updateAttendance")
    public R<String> updateAttendance(@RequestBody CourseAttendance courseAttendance){
        courseAttendanceService.updateById(courseAttendance);
        return R.success("操作成功");
    }

    //查询考勤记录
    @GetMapping("/getAttendance")
    public R<Page<AttendanceDto>> getAttendance(int page,int pageSize,String weekNum,String cId){
        Page<CourseAttendance> attendancePage = new Page<>(page,pageSize);
        Page<AttendanceDto> dtoPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<CourseAttendance> queryWrapper = new LambdaQueryWrapper<>();
        if (weekNum!=null){
            queryWrapper.eq(CourseAttendance::getWeekNum,weekNum);
        }
        if (cId!=null){
            queryWrapper.eq(CourseAttendance::getCourseId,cId);
        }
        courseAttendanceService.page(attendancePage,queryWrapper);
        BeanUtils.copyProperties(attendancePage,dtoPage,"records");
        List<CourseAttendance> records = attendancePage.getRecords();
        List<AttendanceDto> attendanceDtos = records.stream().map(item->{
            AttendanceDto attendanceDto = new AttendanceDto();
            BeanUtils.copyProperties(item,attendanceDto);
            Long courseId = attendanceDto.getCourseId();
            CourseCategory courseCategory = courseCategoryService.getById(courseId);
            attendanceDto.setCName(courseCategory.getCourseName());
            return attendanceDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(attendanceDtos);
        return R.success(dtoPage);
    }

    //添加缺勤记录
    @PostMapping("/attStudent")
    public R<String> saveAttStudent(@RequestBody AbsentStudent absentStudent){
        absentStudentService.save(absentStudent);
        return R.success("操作成功");
    }


    @GetMapping("/getCourse")   //科目
    public R<List<CourseCategory>> getCourse(){
        List<CourseCategory> list = courseCategoryService.list();
        return R.success(list);
    }



    //查询此课程的缺勤人员
    @GetMapping("/findStudent")
    public R<Page<AbsentStudentDto>> find(Long cId,Integer weekNum,Long sId){
        Page<AbsentStudent> studentPage = new Page<>();
        Page<AbsentStudentDto> dtoPage = new Page<>();
        LambdaQueryWrapper<AbsentStudent> wrapper = new LambdaQueryWrapper<>();
        if (cId!=null){
            wrapper.eq(AbsentStudent::getCouAttId,cId);
        }
        if (weekNum!=null){
            wrapper.eq(AbsentStudent::getWeekNum,weekNum);
        }
        if (sId!=null){
            wrapper.eq(AbsentStudent::getStudentId,sId);
        }
        absentStudentService.page(studentPage,wrapper);
        BeanUtils.copyProperties(studentPage,dtoPage,"records");
        List<AbsentStudent> records = studentPage.getRecords();
        List<AbsentStudentDto> dtoPages = records.stream().map(item->{
            AbsentStudentDto studentDto = new AbsentStudentDto();
            BeanUtils.copyProperties(item,studentDto);
            Long studentId = studentDto.getStudentId();
            Long courseId = studentDto.getCourseId();
            Student student = studentService.getById(studentId);
            Course course = courseService.getById(courseId);  //课程表中的课程对象
            studentDto.setSName(student.getStudentName());
            studentDto.setCName(course.getCourseName());
            studentDto.setCTime(course.getCourseTime());
            return studentDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(dtoPages);
        return R.success(dtoPage);
    }
}
